package com.yiku.utils;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * @Author:ludada
 * @Date:2022/11/25 11:24
 */
//代码自动生成器
public class MyBatisPlusAutoCodingUtil {

//    @Value("${spring.datasource.dynamic.datasource.dd.url}")
//    private static String url;
//
//    @Value("${spring.datasource.dynamic.datasource.dd.username}")
//    private static String username;
//
//    @Value("${spring.datasource.dynamic.datasource.dd.password}")
//    private static String password;


    public static void main(String[] args) {
        // 代码生成器 对象
        AutoGenerator mpg = new AutoGenerator();

        /**
         * 全局配置
         */
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("ludada");
        gc.setOpen(false);
         gc.setSwagger2(true); //实体属性 Swagger2 注解
        gc.setIdType(IdType.ASSIGN_ID);
        gc.setDateType(DateType.ONLY_DATE);

        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        //数据库类型
        dsc.setDbType(DbType.SQL_SERVER);
//        dsc.setUrl("jdbc:sqlserver://192.168.5.5:1433;databasename=STU9C");
        dsc.setUrl("jdbc:sqlserver://172.18.1.46:1433;databasename=kh");
        dsc.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dsc.setUsername("sa");
//        dsc.setPassword("abc!@#123");
        dsc.setPassword("zhuzexin321");


//        dsc.setDbType(DbType.MYSQL);
//        dsc.setUrl("jdbc:mysql://localhost:3306/mp?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8");
//        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
//        dsc.setUsername("root");
//        dsc.setPassword("root");
        
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("auto");
        pc.setParent("com.yiku");
        pc.setMapper("mapper");
        pc.setEntity("entity");
        pc.setController("controller");
        pc.setService("service");


        mpg.setPackageInfo(pc);


        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //要映射的表名。之后的主要修改项
//        strategy.setInclude("sync_cgfs_comparison","sync_cggk"
//        ,"sync_dwgk","sync_kygk","sync_role","sync_sscj","sync_sszycj"
//        ,"sync_user","sync_ygzs","sync_zcpp","sync_zsjh");
        strategy.setInclude("PM_RcvLine");
        //下划线转驼峰
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //逻辑删除
        strategy.setLogicDeleteFieldName("deleted");
        //自动填充配置
        TableFill gmtCreate = new TableFill("gmtCreate", FieldFill.INSERT);
        TableFill version = new TableFill("version", FieldFill.INSERT);
        TableFill deleted = new TableFill("deleted", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("gmtModified", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        tableFills.add(version);
        tableFills.add(deleted);
        strategy.setTableFillList(tableFills);
        //乐观锁配置
        strategy.setVersionFieldName("version");
        //开启驼峰
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        //lombok
        strategy.setEntityLombokModel(true);

        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
